/*****************************************************
PROGRAM: 01_MW_FirstStage.do
PURPOSE: Analyze changes in mortality rates
	in treated states compared to untreated
	For Miller & Wherry
DATE:	June 27, 2018
AUTHOR: Sarah Miller, mille@umich.edu
UPDATED:February 22, 2018 Corrected Numident File
	July 7, 2020 by LW to add person identifiers
******************************************************/
global data /projects/mortality/data/
global userdata /projects/mortality/data/
global output /projects/mortality/output/


forval y=2008/2011{
	use $data/pers`y', clear
	describe
	gen year=`y'
  
        gen studyid=_n
	destring rcgp, replace
	*RECODE RACE: LINE REDACTED
	destring his, replace
        *RECODE HISPANIC: LINE REDACTED
	destring sex, replace
	gen female=sex==2

	destring hicov hins4, replace
	gen noinsure=anyhi==0
       
	tab cty, m
	tab st, m
	egen fipscounty=concat(st cty)

	*Not counting New Hampshire as a 2014 Expansion State *        
	gen treated14=0
        replace treated14=1 if inlist(st,"05","04","06","08","09","15")
	replace treated14=1 if inlist(st,"17","19","21","24","27","34")
	replace treated14=1 if inlist(st,"35","32","38","39","41","44","53","54","26")
        gen treated15=0
        replace treated15=1 if inlist(st, "33", "42", "18")

        gen treated16=0
        replace treated16=1 if inlist(st, "02", "30")

	gen treated17=0
	replace treated17=1 if inlist(st, "22")
       
	keep year  noinsure black white hispanic female age ///
	disnum dby msp pwgt fipscounty cty st pov povpi treated14 treated15 treated16 ///
	treated17 died deathyear schl ssi cit age2014 studyid cmid pnum pik

	save $data/persr`y', replace
}

forval y=2012/2017{
	use $data/pers`y', clear
	describe
	gen year=`y'
	*keep if inrange(age,19,64)
        gen studyid=_n
	destring rcgp, replace
        *RECODE RACE: LINE REDACTED
	destring his, replace
        *RECODE HISPANIC: LINE REDACTED
	destring sex, replace
	gen female=sex==2

	destring hicov hins4, replace
	gen anyhi=hicov==1
	gen noinsure=anyhi==0
	gen medicaid=hins4==1

        destring dis, replace
        gen anydis=dis==1
        
	tab cty, m
	tab st, m
	egen fipscounty=concat(st cty)
	*Not counting New Hampshire as a 2014 Expansion State *

    	gen treated14=0
        replace treated14=1 if inlist(st,"05","04","06","08","09","15")
	replace treated14=1 if inlist(st,"17","19","21","24","27","34")
	replace treated14=1 if inlist(st,"35","32","38","39","41","44","53","54","26")
        gen treated15=0
        replace treated15=1 if inlist(st, "33", "42", "18")

        gen treated16=0
        replace treated16=1 if inlist(st, "02", "30")

        gen treated17=0
        replace treated17=1 if inlist(st, "22")
   

	keep year anyhi noinsure medicaid hicov hins4 black white hispanic female age anydis disnum msp ///
	pwgt fipscounty cty st pov povpi zcta5 treated14 treated15 treated16 treated17 schl ///
	ssi cit age2014 deathyear died dby studyid cmid pnum pik

	save $data/persr`y', replace
}

********************************************************************************************************
***A) Prepare data for analysis
forval t=2008/2013 {
forval y=`t'/2017{
use $data/persr`t', clear
gen dyear=`y'
gen diedpanel=0
replace diedpanel=1 if deathyear==`y'
drop if deathyear < `y'
save $data/panel`t'`y', replace
}
}

use $data/panel20082009, clear
append using $data/panel20082010 $data/panel20082011 $data/panel20082012 $data/panel20082013 ///
 $data/panel20082014 $data/panel20082015 $data/panel20082016 $data/panel20082017 $data/panel20092010 ///
$data/panel20092011 $data/panel20092012 $data/panel20092013 $data/panel20092014 $data/panel20092015 ///
$data/panel20092016 $data/panel20092017 $data/panel20102011 $data/panel20102012 $data/panel20102013 ///
$data/panel20102014 $data/panel20102015 $data/panel20102016 $data/panel20102017 $data/panel20112012 ///
$data/panel20112013 $data/panel20112014 $data/panel20112015 $data/panel20112016 $data/panel20112017 ///
$data/panel20122013 $data/panel20122014 $data/panel20122015 $data/panel20122016 $data/panel20122017 ///
$data/panel20132014 $data/panel20132015 $data/panel20132016 $data/panel20132017 $data/panel20082008 ///
$data/panel20092009 $data/panel20102010 $data/panel20112011 $data/panel20122012 $data/panel20132013 
save $data/panel, replace

tab year dyear


use $data/panel, clear

gen countyfips=st+cty
destring countyfips, replace

gen sameyear=0
replace sameyear=1 if year==dyear

gen event4=1*(treated14 & dyear==2017)
gen event3=1*(treated14 & dyear==2016)
gen event2=1*(treated14 & dyear==2015)
gen event1=1*(treated14 & dyear==2014)
gen event0=1*(treated14 & dyear==2013)
gen eventm1=1*(treated14 & dyear==2012)
gen eventm2=1*(treated14 & dyear==2011)
gen eventm3=1*(treated14 & dyear==2010)
gen eventm4=1*(treated14 & dyear==2009)
gen eventm5=1*(treated14 & dyear==2008)

replace event3=1 if treated15==1 & dyear==2017
replace event2=1 if treated15==1 & dyear==2016
replace event1=1 if treated15==1 & dyear==2015
replace event0=1 if treated15==1 & dyear==2014
replace eventm1=1 if treated15==1 & dyear==2013
replace eventm2=1 if treated15==1 & dyear==2012
replace eventm3=1 if treated15==1 & dyear==2011
replace eventm4=1 if treated15==1 & dyear==2010
replace eventm5=1 if treated15==1 & dyear < 2010

replace event2=1 if treated16==1 & dyear==2017
replace event1=1 if treated16==1 & dyear==2016
replace event0=1 if treated16==1 & dyear==2015
replace eventm1=1 if treated16==1 & dyear==2014
replace eventm2=1 if treated16==1 & dyear==2013
replace eventm3=1 if treated16==1 & dyear==2012
replace eventm4=1 if treated16==1 & dyear==2011
replace eventm5=1 if treated16==1 & dyear < 2011

drop treated17
gen treated17=0
replace treated17=1 if inlist(st, "22")

replace event1=1 if treated17==1 & dyear==2017
replace event0=1 if treated17==1 & dyear==2016
replace eventm1=1 if treated17==1 & dyear==2015
replace eventm2=1 if treated17==1 & dyear==2014
replace eventm3=1 if treated17==1 & dyear==2013
replace eventm4=1 if treated17==1 & dyear==2012
replace eventm5=1 if treated17==1 & dyear < 2012

drop if povpi==.
destring st, replace
drop if inlist(st, 11,10,25,36,50)

destring schl, replace

gen target1=0
replace target1=1 if povpi < 139
replace target1=1 if schl < 16
replace target1=0 if ssi > 0
replace target1=0 if cit=="5"
replace target1=0 if age2014 > 64

gen target=target1

gen preexp=0
replace preexp=1 if treated14==1 & dyear==2013
replace preexp=1 if treated15==1 & dyear==2014
replace preexp=1 if treated16==1 & dyear==2015
replace preexp=1 if treated17==1 & dyear==2016


gen treated14controls=treated14
replace treated14controls=1 if treated15==0 & treated16==0 & treated17==0

tab dyear year

gen lths=0
replace lths=1 if schl < 16


gen posttreated=0
replace posttreated=1 if treated14==1 & dyear > 2013
replace posttreated=1 if treated15==1 & dyear > 2014
replace posttreated=1 if treated16==1 & dyear > 2015
replace posttreated=1 if treated17==1 & dyear > 2016

gen blacknh=black-hispanic
replace blacknh=0 if blacknh < 0

gen whitenh=white-hispanic
replace whitenh=0 if whitenh < 0

gen postperiod=0
replace postperiod=1 if dyear > 2013 & treated14==1
replace postperiod=1 if dyear > 2014 & treated15==1
replace postperiod=1 if dyear > 2015 & treated16==1
replace postperiod=1 if dyear > 2016 & treated17==1
save $data/panelfinal, replace

***count number of deaths at the county level for main analytic sample

use $data/panelfinal, clear
keep if target==1 & age2014 > 54
collapse diedpanel, by(countyfips dyear)
save $data/countymort.dta

**prepare data for propensity score estimation
global econcty bartik_allind unemp_rate medhhinc povrate sharefemale shareblack sharewhite sharehispanic tradeotch2000_2014
global statemiss mustaccess pdmp triplicate painlaw medmj dispmj drugodratest opioidrateaa opioidchangeaa tradeotch2000_2014 stunemp
*global statemiss mustaccess pdmp triplicate painlaw medmj dispmj drugodratest tradeotch2000_2014 stunemp 
global countymiss povrate medhhinc sharefemale shareblack sharewhite sharehispanic pct* bartik_allind drugodrate 
global countymisspop poptotal pop20_24 pop25_29 pop30_34 pop35_39 pop40_44 pop45_49 pop50_54 pop55_59 pop60_64 
global characteristics $statemiss $countymiss  
global matchingchar povrate medhhinc unemp_rate bartik_allind mustaccess pdmp triplicate painlaw medmj dispmj changedied0809 changedied0910 changedied1011 

*Merge in external county level variables

use /projects/dstafftransfer/transfer.20200130/controlvars.dta, clear
drop poptotal sharefemale shareblack sharewhite sharehispanic
merge 1:1 countyfips year using /projects/dstafftransfer/transfer.20200213/controlvars2.dta
tab _merge
drop _merge
drop if year==2018
rename year dyear
sort dyear
merge 1:1 countyfips dyear using /projects/mortality/data/countymort.dta
tab _merge
drop _merge
replace diedpanel=0 if diedpanel==.
forval n=2008/2013{
gen dieda`n'=diedpanel if dyear==`n'
bys countyfips: egen died`n'=max(dieda`n')
drop dieda`n'
}
summ died*
gen changedied0809=died2009-died2008
gen changedied0910=died2010-died2009
gen changedied1011=died2011-died2010
gen changedied1112=died2012-died2011
gen changedied1213=died2013-died2012
summ change*
replace povrate=povrate/poptotal

**Missings coded as no change in labor demand
replace bartik_allind=1 if dyear < 2018 & bartik_allind==.

**No Alaska or Hawaii for the trade measures
bys dyear: summ $econopioidcty

fillin countyfips dyear

replace drugodrate=0 if drugodrate==. & dyear < 2017
replace drugodst=0 if drugodst==. & dyear < 2017

gen fr2024=pop20_24/poptotal
gen fr2529=pop25_29/poptotal
gen fr3034=pop30_34/poptotal
gen fr3539=pop35_39/poptotal
gen fr4044=pop40_44/poptotal
gen fr4549=pop45_49/poptotal
gen fr5054=pop50_54/poptotal
gen fr5559=pop55_59/poptotal
gen fr6064=pop60_64/poptotal


replace state_fips=15 if countyfips==15005
replace state_fips=51 if countyfips==51515
replace state_fips=2 if countyfips==2105
replace state_fips=2 if countyfips==2275
replace state_fips=2 if countyfips==2232
replace state_fips=2 if countyfips==2280
replace state_fips=2 if countyfips==2201
replace state_fips=2 if countyfips==2198
replace state_fips=2 if countyfips==2270
replace state_fips=2 if countyfips==2230
replace state_fips=2 if countyfips==2195
replace state_fips=46 if countyfips==46113

foreach x of var $statemiss {
bys state_fips dyear: egen max`x'=max(`x')
replace `x'=max`x' if `x'==. & dyear < 2018
}

replace unemp_rate=stunemp if unemp_rate==.
bys state_fips dyear: egen statepop=total(poptotal)

foreach x of var $countymiss {
gen wt`x'=`x'*poptotal
bys state_fips dyear: egen `x'sum=total(wt`x')
gen `x'state=`x'sum/statepop
drop `x'sum wt`x'
replace `x'=`x'state if `x'==.
}

foreach x of var $countymisspop {
bys state_fips dyear: egen `x'avg=mean(`x')
replace `x'=`x'avg if `x'==.
}

bys state_fips: egen drugodprest1=mean(drugodratest) if dyear < 2011
bys state_fips: egen drugodprest=max(drugodprest1)
drop drugodprest1

bys countyfips: egen drugodpre1=mean(drugodrate) if dyear < 2011
bys countyfips: egen drugodpre=max(drugodpre1)
drop drugodpre1

bys dyear: summ $characteristics

save /projects/mortality/data/controlvars.dta, replace

**Create propensity score weights:
keep if dyear < 2014

collapse $matchingchar drugodpre drugodprest changedied1112 changedied1213 [pweight=poptotal], by(countyfips state_fips)

**Define expansion states (any states who expand over our period)
rename state_fips st
gen treated=0
 replace treated=1 if inlist(st,5,4,6,8,9,15)
 replace treated=1 if inlist(st,17,19,21,24,27,34) 
 replace treated=1 if inlist(st, 35,32,38,39,41,44,53,54,26)
 replace treated=1 if inlist(st, 33,42,18)
 replace treated=1 if inlist(st, 2,30)
 replace treated=1 if inlist(st, 22)

*Drop early expanders
replace treated=. if inlist(st, 11,10,25,36,50)

lasso linear treated $matchingchar drug*
di e(allvars_sel)

dprobit treated povrate unemp_rate bartik_allind mustaccess pdmp triplicate painlaw medmj dispmj changedied0809 changedied1011 drugodpre, robust
predict ps,pr

gen ps_wt=1 if treated==1
replace ps_wt=(ps/(1-ps)) if treated==0

bys treated: summ $matchingchar [aweight=ps_wt]

rename st state_fips
keep ps* ps_wt* countyfips state_fips
merge 1:m countyfips state_fips using /projects/mortality/data/controlvars.dta
drop _merge

save /projects/mortality/data/controlvars.dta, replace

***merge on control variables
use $data/panelfinal, clear
merge m:1 countyfips dyear using /projects/mortality/data/controlvars.dta
tab _merge

gen pwgt2=pwgt*ps_wt

save $data/panelfinal, replace

***add SHADAC family income definition 
forval x=2008/2013{
  use $data/pers`x', clear

  ***A) Create SPLOC, MOMLOC, and POPLOC variables 
  gen year=`x'
  egen hhid=concat(year cmid)
  egen famid=concat(year cmid sfn)
  gen child=age<=18
  gen momloc=0 
  gen poploc=0
  gen sploc=0

  destring rel sex pnum, replace

  *i) use household relationship variable rel to identify parents 
  gen dum=1*(rel==0 & sex==2)
  bysort hhid: egen fhh=max(dum)
  drop dum 
  gen dum=1*(rel==0 & sex==1)
  bysort hhid: egen dhh=max(dum)
  drop dum
  gen dum=1*(rel==1 & sex==2)
  bysort hhid: egen wsp=max(dum)
  drop dum
  gen dum=1*(rel==1 & sex==1)
  bysort hhid: egen dsp=max(dum)
  drop dum

  gen dum=1*(rel==0)
  replace dum=dum*pnum
  bysort hhid: egen hhp=max(dum)
  drop dum
  gen dum=1*(rel==1)
  replace dum=dum*pnum
  bysort hhid: egen spp=max(dum)
  drop dum

  replace poploc=hhp if dhh & inrange(rel,2,4)
  replace momloc=hhp if fhh & inrange(rel,2,4)
  replace momloc=spp if wsp & inrange(rel,2,4) 
  replace poploc=spp if dsp & inrange(rel,2,4) 

  gen dum=1*(rel==6 & sex==2)
  bysort hhid: egen fpar=max(dum)
  replace dum=dum*pnum 
  bys hhid: egen farp=max(dum)
  drop dum
  gen dum=1*(rel==6 & sex==1)
  bysort hhid: egen mpar=max(dum)
  replace dum=dum*pnum
  bys hhid: egen marp=max(dum)
  drop dum

  replace momloc=farp if fpar & inlist(rel,0,5)
  replace poploc=marp if mpar & inlist(rel,0,5)

  gen dum=1*(rel==8 & sex==2)
  bysort hhid: egen finl=max(dum)
  replace dum=dum*pnum
  bys hhid: egen finlp=max(dum)
  drop dum
  gen dum=1*(rel==8 & sex==1)
  bysort hhid: egen minl=max(dum)
  replace dum=dum*pnum
  bys hhid: egen minlp=max(dum)
  drop dum

  replace momloc=finlp if finl & inlist(rel,1)
  replace poploc=minlp if minl & inlist(rel,1)

  **ii) use subfamily relationship var sfr to identify parent-child relationships
  destring sfr, replace
  gen dum=1*(sfr==3 & sex==2)
  bysort famid: egen parf=max(dum)
  replace dum=dum*pnum
  bys famid: egen parfp=max(dum)
  drop dum
  gen dum=1*(sfr==3 & sex==1)
  bysort famid: egen parm=max(dum)
  replace dum=dum*pnum
  bys famid: egen parmp=max(dum)
  drop dum

  replace momloc=parfp if parf & sfr==5
  replace poploc=parmp if parm & sfr==6

  gen dum=1*(sfr==2 & sex==2)
  bysort famid: egen marcf=max(dum)
  replace dum=dum*pnum
  bys famid: egen marcfp=max(dum)
  drop dum
  gen dum=1*(sfr==2 & sex==1)
  bysort famid: egen marcm=max(dum)
  replace dum=dum*pnum
  bys famid: egen marcmp=max(dum)
  drop dum

  replace momloc=marcfp if marcf & sfr==4
  replace poploc=marcmp if marcm & sfr==4

  **iii) use household rel var to identify spouse
  gen dum=1*(rel==1)
  bysort hhid: egen sphh=max(dum)
  replace dum=dum*pnum 
  bysort hhid: egen sphhp=max(dum)
  drop dum
  gen dum=rel==0
  replace dum=dum*pnum
  bysort hhid: egen sphhp2=max(dum)
  drop dum

  replace sploc=sphhp if rel==0 & sphh==1
  replace sploc=sphhp2 if rel==1 & sphh==1

  *iv) use subfamily relationship var sfr to identify spouse
  gen dum=inlist(sfr,1,2)
  bysort famid: egen spfam=max(dum)
  drop dum
  sort famid sfr pnum
  bysort famid: gen count=_n
  su count if inlist(sfr,1,2)
  gen dum=(inlist(sfr,1,2) & count==1)
  replace dum=dum*pnum
  bysort famid: egen sphfamp=max(dum)
  drop dum
  gen dum=(inlist(sfr,1,2) & count==2)
  replace dum=dum*pnum
  bysort famid: egen sphfamp2=max(dum)
  drop dum

  replace sploc=sphfamp if inlist(sfr,1,2) & count==2
  replace sploc=sphfamp2 if inlist(sfr,1,2) & count==1

  *note: 2012 file has a handful of duplicates
  sort hhid pnum
  egen flag=tag(hhid pnum)
  keep if flag
  drop flag
  save $data/hiu`x', replace

  *v) assign number of own children to parents
  use $data/hiu`x', clear
  keep if momloc!=0
  drop pnum
  rename momloc pnum
  gen ob=1
  collapse (sum) nchild=ob, by(hhid pnum)
  merge 1:1 hhid pnum using $data/hiu`x', keep(match using) nogen
  save $data/hiu`x', replace
  keep if poploc!=0
  drop pnum
  rename poploc pnum
  gen ob=1
  collapse (sum) nchild2=ob, by(hhid pnum)
  merge 1:1 hhid pnum using $data/hiu`x', keep(match using) nogen 
  replace nchild=nchild2 if nchild==. 
  drop nchild2
  save $data/hiu`x', replace


  ***B) Create health insurance unit (HIU) ID variable
  ***	Adaptation of code for IPUMS from SHADAC 
  destring msp, replace
  gen echild=(age<=18 & ~inlist(msp,1,2) & nchild==0)
  lab var echild "Eligible to be linked to HIU as Child"

  gen rel_echild=(echild==1 & inrange(rel,2,10))
  lab var rel_echild "Related Eligible Child"

  bysort famid: egen anyechild=max(echild)

  egen persid=concat(year cmid pnum)

  gen hiup=.
  replace hiup=1 if (msp==1 & sex==1) | ///
  (sex==1 & nchild>0 & msp!=1) | ///
  (sex==2 & nchild>0 & msp!=1)
  replace hiup=2 if (msp==1 & sex==2)
  replace hiup=3 if echild==1 & (poploc!=0 | momloc!=0)
  replace hiup=4 if msp!=1 & echild==0 & nchild==0
  replace hiup=5 if echild==1 & hiup==. & rel_echild==1
  replace hiup=6 if echild==1 & hiup==. & rel_echild==0
  lab var hiup "HIU Person Type"

  gen hiu_ref=(hiup==1)
  label var hiu_ref "HIU Reference Person"

  egen hiu_refhhld=max(hiu_ref), by(hhid)
  lab var hiu_refhhld "HIUP==1 Present in HH"

  egen first_hiuref=min(pnum) if hiup==1, by(hhid)
  egen first_point=min(first_hiuref), by(hhid)
  replace first_point=1 if first_point==.
  label var first_point "Point to First HIU REF of HH"

  gen hiu_point_rule=1

  gen hiu_point=pnum if hiup==1
  replace hiu_point_rule=1 if hiu_point!=.

  replace hiu_point=sploc if hiup==2 & sploc>0
  replace hiu_point=pnum if hiup==2 & sploc==0
  replace hiu_point_rule=2 if hiu_point!=. & hiu_point_rule==.

  replace hiu_point=poploc if hiup==3 & hiu_point==. & poploc!=0
  replace hiu_point_rule=3 if hiu_point!=. & hiu_point_rule==.

  replace hiu_point=momloc if hiup==3 & hiu_point==. & momloc!=0
  replace hiu_point_rule=4 if hiu_point!=. & hiu_point_rule==. 

  replace hiu_point=pnum if hiup==4
  replace hiu_point_rule=5 if hiu_point!=. & hiu_point_rule==. 

  replace hiu_point=first_point if hiup==5
  replace hiu_point_rule=6 if hiu_point!=. & hiu_point_rule==.

  replace hiu_point=pnum if hiup==6
  replace hiu_point_rule=7 if hiu_point!=. & hiu_point_rule==. 

  lab var hiu_point "HIU Ref Pointer"
  lab var hiu_point_rule "HIU Pointer Rule"
  lab def hiu_point_rule 1 "Referent" 2 "Married Dependent to Spouse Ref" ///
  3 "Child to Father" 4 "Child to Single Mother" 5 "Single Adult" ///
  6 "Point to Ref: Related Single Child" 7 "Single Child", modify
  lab val hiu_point_rule hiu_point_rule

  tostring (hiu_point), gen(hiu_point_s) format(%02.0f)

  egen hiu_id=concat(hhid hiu_point)
  lab var hiu_id "HIU ID"

  capture drop famsize
  gen ob=1
  bysort hiu_id: egen famsize=total(ob)
  gen kid=1*(inrange(age,0,18))
  bysort hiu_id: egen skid=total(kid)
  gen parent=skid>0
  recode pinc (-999999999/-0=0), gen(rpi)
  bysort hiu_id: egen finc=total(pinc)
  bysort hiu_id: egen fampa=total(pa)
  lab var fampa "Family public assistance income"
  bysort hiu_id: egen famssi=total(ssi)
  lab var famssi "Family SSI income"
  replace finc=finc-fampa-famssi
  replace finc=0 if finc<0 | finc==. 
  label var finc "Family income net of TANF/SSI"

  keep year cmid pnum hiu_id famsize finc skid parent fampa famssi hiu_point ///
  hiu_point_rule sploc momloc poploc
  
  save $data/hiu`x', replace
}

use $data/hiu2008, clear
append using $data/hiu2009 $data/hiu2010 $data/hiu2011 $data/hiu2012 $data/hiu2013

duplicates report year cmid pnum 
duplicates tag year cmid pnum, gen(dup)
tab pnum if dup>0
tab cmid if dup>0
tab year if dup>0
drop if dup>0

merge 1:m year cmid pnum using $data/panelfinal, keep(match) nogen

save $data/panelfinalelig, replace
  
gen fpl=.
replace fpl=10400+(famsize-1)*3600 if year==2008
replace fpl=10830+(famsize-1)*3740 if inlist(year,2009,2010)
replace fpl=10890+(famsize-1)*3820 if year==2011
replace fpl=11170+(famsize-1)*3960 if year==2012
replace fpl=11490+(famsize-1)*4020 if year==2013

replace fpl=13000+(famsize-1)*4500 if year==2008 & st==2
replace fpl=13530+(famsize-1)*4680 if inlist(year,2009,2010) & st==2
replace fpl=13600+(famsize-1)*4780 if year==2011 & st==2
replace fpl=13970+(famsize-1)*4950 if year==2012 & st==2
replace fpl=14350+(famsize-1)*5030 if year==2013 & st==2

replace fpl=11960+(famsize-1)*4140 if year==2008 & st==15
replace fpl=12460+(famsize-1)*4300 if inrange(year,2009,2010) & st==15
replace fpl=12540+(famsize-1)*4390 if year==2011 & st==15
replace fpl=12860+(famsize-1)*4550 if year==2012 & st==15
replace fpl=13230+(famsize-1)*4620 if year==2013 & st==15

gen fips=st

*new target using SHADAC definition of family unit for income calc
gen newtarget=0
replace newtarget=1 if finc/fpl<=1.38
replace newtarget=1 if schl<16
replace newtarget=0 if ssi>0
replace newtarget=0 if cit=="5"
replace newtarget=0 if age2014>64

*checks
corr newtarget target1
tab newtarget target1
gen def1=povpi<139
gen def2=finc/fpl<=1.38
tab def1 def2, row col 

save $data/panelfinalelig, replace


